package com.shenzhen.horselive.utils;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.widget.TextView;

import java.text.NumberFormat;

public class AnimationUtils {
    /**
     * @param tv    TextView
     * @param value 最终显示的值
     */
    public static void addTextViewAddAnim(TextView tv, double value) {
        TextViewEvaluator evaluator = new TextViewEvaluator(value,"","");
        ValueAnimator animator = ValueAnimator.ofObject(evaluator, tv);
        //动画时间
        animator.setDuration(2000);
        animator.start();
    }

    /**
     * @param tv    TextView
     * @param value 最终显示的值
     * @param startString 前缀
     */
    public static void addTextViewAddAnim(TextView tv, double value,String  startString) {
        TextViewEvaluator evaluator = new TextViewEvaluator(value,startString,"");
        ValueAnimator animator = ValueAnimator.ofObject(evaluator, tv);
        //动画时间
        animator.setDuration(2000);
        animator.start();
    }

    /**
     * @param tv    TextView
     * @param value 最终显示的值
     * @param endString 猴嘴
     */
    public static void addTextViewAddAnim(TextView tv, double value,String startString, String  endString) {
        TextViewEvaluator evaluator = new TextViewEvaluator(value,startString,endString);
        ValueAnimator animator = ValueAnimator.ofObject(evaluator, tv);
        //动画时间
        animator.setDuration(2000);
        animator.start();
    }


    //核心类
    static class TextViewEvaluator implements TypeEvaluator {
        private double value = 0;
        private  String   startString ;
        private String    endString;

        TextViewEvaluator(double value,String  startString,String  endString) {
            this.value = value;
            this.startString =  startString;
            this.endString = endString;
        }

        @Override
        public Object evaluate(float fraction, Object startValue, Object endValue) {
            //样式具体改变 (自定义)
            TextView tv = (TextView) endValue;
            NumberFormat format = NumberFormat.getInstance();
            format.setMaximumFractionDigits(10);
            tv.setText(startString+format.format(value * fraction)+endString);
            return startValue;
        }

    }

}
